#!/usr/bin/env python
# $Id: shopping_list.py 2 2008-04-04 18:53:47Z benchline $

import datetime
from page import page
import sys, time, urllib
import db 

class add(page):

	def __init__(self):
		page.__init__(self)
		self.headingstr = "Check Out/Shopping List Report"
		self.db1 = db.db()
		self.upcdb1 = db.upcdb()

	def render(self):
		self.logic()
		self.encoding()
		self.html()

	def logic(self):
		if request.has_key("cancel"):
			raise HTTP_REDIRECTION, "menu.py"
		days_ago = request["days_ago"][0]
		self.details = self.db1.get_checked_out_list(days_ago)

	def show_detail(self):
		"""
		Show the detail
		"""
		print """<a href=reports.py>Reports</a>
				| <a href=menu.py>Menu</a>"""
		today = datetime.date.today()
		delta = datetime.timedelta(days=int(request["days_ago"][0]))
		old_date = today - delta
		date_format = "%A %b %d, %Y"
		print """<br>Items Checked Out from<br>%s through %s""" % (old_date.strftime(date_format), 
			today.strftime(date_format))
		print """<table border=1 cellpadding=3 cellspacing=0>
		<tr>
			<td>Category</td>
			<td>Item Name</td>
			<td>Count</td>
		</tr>
		"""
		for line in self.details:
			print """<tr>
						<td>%s</td>
						<td>%s</td>
						<td>%s</td>
					</tr>""" % line
		print "</table>"

	def message(self):
		if request.has_key("m"):
			message = request["m"][0]
			if message:
				print message

	def body(self):
		self.start_body()
		self.heading()
		self.message()
		self.show_detail()
		self.footer()
		self.end_body()

class runner:
	def _test(self):
		add().render()

	def _doctest(self):
		import doctest
		doctest.testmod()

if __name__ == "__main__":
	r = runner()
	getattr(r, "_"+sys.argv[1])()
else:
	add().render()
